package 极客算法训练营.chapter03;

public class 加一 {
    public static void main(String[] args) {
    }

    public int[] plusOne(int[] digits) {
        int index = digits.length - 1;
        int[] result = digits;
        digits[index]++;
        while (index > 0) {
            if (digits[index] == 10) {
                digits[index] = 0;
                digits[--index]++;
            } else {
                break;
            }
        }
        if (digits[0] == 10) {
            result = new int[digits.length + 1];
            result[0] = 1;
            result[1] = 0;
            for (int i = 1; i < digits.length; i++) {
                result[i + 1] = digits[i];
            }
        }
        return result;
    }
}
